package com.study.csv;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * <p>添加类描述</p>
 *
 * @author chentao
 * @version v 0.1 2024/6/17 16:01
 */
public class CsvTest2 {
    public static void main(String[] args) throws IOException {
       // test1();
        test2();
    }

    private static void test2() throws IOException {
        String v1= "name1:v1";
        String v2= "name2:v2";
        File file = new File("C:\\test\\test2.csv");
        FileOutputStream out = new FileOutputStream(file);
        String row5Pattern = "\t{0},\t{1}\r\n";
        Object[] row5Date = new Object[]{"\t姓名", "\t" + "年级"};

        out.write(MessageFormat.format(row5Pattern, row5Date).getBytes("GBK"));
        ArrayList<Person> peoples= new ArrayList<>();
        Person person = new Person();
        person.setName("1");
        person.setAge("1");
        person.setName1("setName1");
        person.setName2("setName2");
        peoples.add(person);
        for (Person people : peoples) {
            Object[] objects = {people.getAge(), people.getName()};
            out.write(MessageFormat.format(row5Pattern, objects).getBytes("GBK"));
        }
    }

    private static void test1() throws IOException {
        String v1= "name1:v1";
        String v2= "name2:v2";
        File file = new File("C:\\test\\test1.csv");
        FileOutputStream out = new FileOutputStream(file);
        String row5Pattern = "\t{0},\t{1}\r\n";
        String row5PatternCode = "\t{0:name},\t{1:age}";
        Object[] row5Date = new Object[]{"\t姓名", "\t" + "年级"};
        if (StringUtils.isNotEmpty(v1)) {
            row5Pattern = row5Pattern.replaceAll("\\r\\n","")+"";
            row5PatternCode=row5PatternCode+",\t{1:age}\r\n";
            row5Date[row5Date.length]="name1";
        }
        if (v2.equals(v2)) {
            row5Date[row5Date.length]="name2";
        }
        out.write(MessageFormat.format(row5Pattern, row5Date).getBytes("GBK"));
        ArrayList<Person> peoples= new ArrayList<>();
        Person person = new Person();
        person.setName("1");
        person.setAge("1");
        person.setName1("1-2");
        peoples.add(person);
        for (Person people : peoples) {
            Object[] objects = {people.getAge(), people.getName()};
            out.write(MessageFormat.format(row5Pattern, objects).getBytes("GBK"));
        }
    }
}
